package org.s3.project.controller;

import org.apache.ibatis.annotations.Param;
import org.s3.project.common.R;
import org.s3.project.entity.Orders;
import org.s3.project.entity.Shoshoping;
import org.s3.project.entity.Orders;
import org.s3.project.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cfx
 * @version 1.0
 * @Description
 * @date 2025/2/9 9:32
 */
@CrossOrigin(origins = "*")
@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    //根据用户id查询订单所有
    @GetMapping("/selectorders")
    public R selectOrders(@RequestParam("id") int id) {
//        List<Map<String, Object>> list = ordersService.selectOrders(id);
        List<Orders> list= ordersService.selectOrders(id);
        List<Shoshoping>shoshopingList=new ArrayList<Shoshoping>();
       for(Orders orders:list){
           Shoshoping shoshoping = new Shoshoping(
                   orders.getO_amount(),
                   orders.getO_id(),
                   orders.getD_name(),
                   orders.getDish_id(),
                   orders.getImage(),
                   orders.getNumber(),
                   orders.getD_price(),
                   orders.getO_number()
           );

           shoshopingList.add(shoshoping);

       }
        return new R().add("data", list).add("shoshopingList", shoshopingList);
    }
    //评价完后修改他的评价状态为一
    @GetMapping("updatepingjia")
    public Boolean updateOrders(@RequestParam("id") String id) {
        Boolean updatepinglun = ordersService.updatepinglun(id);
        return  updatepinglun;
    }
    //查看待评论
    @GetMapping("ShowEvaluate")
R ShowEvaluate(@Param("id")int id){
    List<Shoshoping>shoshopingList=new ArrayList<Shoshoping>();
    List<Orders> list = ordersService.ShowEvaluate(id);
    for(Orders orders:list){
        Shoshoping shoshoping = new Shoshoping(
                orders.getO_amount(),
                orders.getO_id(),
                orders.getD_name(),
                orders.getDish_id(),
                orders.getImage(),
                orders.getNumber(),
                orders.getD_price(),
                orders.getO_number()
        );
        System.out.println(shoshoping.getOid());
        shoshopingList.add(shoshoping);

}  return new R().add("data", list).add("pinglunList", shoshopingList);
    }

    //退款
    @GetMapping("tuikuanOrders")
    public  Boolean Tuikuan(String oid,String refund_reason){
   return   ordersService.Tuikuan(oid,refund_reason);
    }
    //查看退款原因
    @GetMapping("ShowTuiKuan")
  public  R  ShowTuiKuan(int id){
        List<Shoshoping>shoshopingList=new ArrayList<Shoshoping>();
        List<Orders> list = ordersService.ShowTuiKuan(id);
        for(Orders orders:list){
            Shoshoping shoshoping = new Shoshoping(
                    orders.getO_amount(),
                    orders.getO_id(),
                    orders.getD_name(),
                    orders.getDish_id(),
                    orders.getImage(),
                    orders.getNumber(),
                    orders.getD_price(),
                    orders.getO_number()
            );

            shoshopingList.add(shoshoping);

    }
        return  new R().add("add", list).add("ShowTuikuanlist", shoshopingList);
    }
    //查看进行中
    @GetMapping("ShowProgress")
    public  R  ShowProgress(int id){
        List<Shoshoping>shoshopingList=new ArrayList<Shoshoping>();
        List<Orders> list = ordersService.ShowProgress(id);
        for(Orders orders:list){
            Shoshoping shoshoping = new Shoshoping(
                    orders.getO_amount(),
                    orders.getO_id(),
                    orders.getD_name(),
                    orders.getDish_id(),
                    orders.getImage(),
                    orders.getNumber(),
                    orders.getD_price(),
                    orders.getO_number()
            );
            shoshopingList.add(shoshoping);
        }
        return  new R().add("data", list).add("ShowProgresslist", shoshopingList);
    }
    //取消订单
    @GetMapping("cancelOrder")
    Boolean cancelOrder(@Param("cancel_reason") String cancel_reason ,@Param("id") int id ){
        return ordersService.cancelOrder(cancel_reason,id);
    }
    /**
     *
     * 获取订单总数
     * @return 订单总数
     */
    @GetMapping("getOrdersCount")
    public Map<String, Object> getOrdersCount() {
        long orderCount = ordersService.count();
        Map<String, Object> result = new HashMap<>();
        result.put("orderCount", orderCount);
        return result;
    }
    @PostMapping("insertOrdersBy")
    public Boolean  insertOrdersBy(@RequestBody Orders orders) {

        return ordersService.insertOrdersBy(orders);
    }


    //根据商家id查询订单
    @GetMapping("getOrdersByUserId/{merchant_id}")
    public List<Orders> getOrdersByUserId(@PathVariable("merchant_id")int merchant_id) {
        return ordersService.getOrdersByUserId(merchant_id);
    }
    //修改状态
    @GetMapping("updateStatus/{status}/{merchant_id}")
    public void updateStatus(@PathVariable("status")int status,@PathVariable("merchant_id")int merchant_id){
        ordersService.updateStatus(status,merchant_id);
    }
    //查询商家当日订单
    @GetMapping("getTodayOrders/{merchant_id}")
    public List<Orders> getTodayOrders(@PathVariable("merchant_id")int merchant_id){
        return ordersService.getTodayOrders(merchant_id);
    }
    //订单月销售额明细
    @GetMapping("selectMerchantMonthSalesDetail/{merchant_id}")
    public List<Orders> selectMerchantMonthSalesDetail(@PathVariable("merchant_id")int merchant_id){
        return ordersService.selectMerchantMonthSalesDetail(merchant_id);
    }

}
